package com.baoyouqun.API.wxPay;

import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.RSAPublicKeyNotificationConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 微信支付配置类（适配JsapiServiceExtension源码）
 */
@Configuration
@Data
public class WxPayConfig {

    public static final String PUB_KEY_ID = "PUB_KEY_ID_0115861426712025100900291981003200";
    @Value("${wechat.pay.appid}")
    private String appid;
    @Value("${wechat.pay.mchid}")
    private String mchid;
    @Value("${wechat.pay.apiV3key}")
    private String apiV3Key;
    @Value("${wechat.pay.notifyUrl}")
    private String notifyUrl;
    @Value("${wechat.pay.merchantSerialNumber}")
    private String merchantSerialNumber;
    @Value("${wechat.pay.privateKey}")
    private String privateKey;
    @Value("${wechat.pay.publicKey}")
    private String publicKey;

    @Bean
    public RSAPublicKeyConfig rSAPublicKeyConfig() {

        // 构建配置：注意删除 merchantSerialNumber（RSA公钥场景无需此参数）
        return new RSAPublicKeyConfig.Builder()
                .merchantId(mchid) //微信支付的商户号
                .privateKey(privateKey) // 商户API证书私钥的存放路径
                .publicKey(publicKey) //微信支付公钥的存放路径
                .publicKeyId(PUB_KEY_ID) //微信支付公钥ID
                .merchantSerialNumber(merchantSerialNumber) //商户API证书序列号
                .apiV3Key(apiV3Key) //APIv3密钥
                .build();
    }

    /**
     * 创建JsapiServiceExtension（核心支付服务，适配源码Builder）
     */
    @Bean
    public JsapiServiceExtension jsapiServiceExtension(RSAPublicKeyConfig config) {
        return new JsapiServiceExtension.Builder()
                .config(config)            // 注入配置
                .build();
    }

    @Bean
    public NotificationConfig notificationConfig() {
        return new RSAPublicKeyNotificationConfig.Builder()
                .publicKey(publicKey)
                .publicKeyId(PUB_KEY_ID)
                .apiV3Key(apiV3Key)
                .build();
    }


}